

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException;

/*@Sangeeta
 *@Uses: 1. To get file names from the source code of the branches downloaded.
 *       2. Inserting into desired tables;
 *@
 */

public class a_file_extraction {
	
	public File rootDir=new File("/home/sangeeta/2Research/apache_data/");
	public String database="2apache";
	public String user="root";
	public String password="123";
	public Connection conn = null;
	
	public ArrayList<String> table_names = null;  
	
	public ArrayList<ArrayList<file_object>> all_branch_info  =  null; 
	public ArrayList<file_object> one_branch_info =null;
    public String current_table = "";
	 


public ArrayList<String> getTableNames(File rootDir) 
{
  File file_list [] = rootDir.listFiles();
  
  table_names =  new ArrayList<String>();
  for(int i=0;i<file_list.length;i++)
  {
	  String  file_path = file_list[i].toString();
	  int s1 = file_path.lastIndexOf("/"); 
	  String table_name = file_path.substring(s1+1).trim();
	  table_names.add(table_name);
	  
	  System.out.println("Table Name="+table_name);
  }
  
  return table_names;
  
}//	

//
private void parse(ArrayList<String> table_names,File rootDir) 
{
	File file_list[]  = rootDir.listFiles();
	all_branch_info =  new ArrayList<ArrayList<file_object>>();
	
	for(int i=0;i<file_list.length;i++)
	{
		File branch_file = file_list[i];
		
		String table_path = branch_file.toString();
		int index =  table_path.lastIndexOf("/");
		current_table = table_path.substring(index+1).trim();
		current_table=format(current_table);//format the table name to match the name given by me
	    	
        all_branch_info.add(get_files(branch_file));
        System.out.println("processing="+table_path);
        insert_into_table(all_branch_info.get(i));
         
	}//for
	
	for(int i=0;i<all_branch_info.size();i++)
	{
		//insert_into_table(all_branch_info.get(i));
	}
	
}//


private void insert_into_table(ArrayList<file_object> one_branch_info)
{
   
	Statement stmt =  null;
	
	try 
	{
	    	stmt =  conn.createStatement();
	    	String table_name =  one_branch_info.get(0).table_name;
	        for(int i=0;i<one_branch_info.size();i++)
	        {
	          String file_name =  one_branch_info.get(i).file_name;
	          String file_path = one_branch_info.get(i).file_path;
	          int file_no = one_branch_info.get(i).file_no;
	          String fid = "fid"+(i+1);
	          
	          String insert=  "insert  into "+ table_name +" values('"+table_name+"','"+fid+"','"+file_name+"',"+file_no+",'"+file_path+"')";
	     
	          try
	           {
	             stmt.executeUpdate(insert);
	           }catch(SQLException e)
	           {
	        	   e.printStackTrace();
	        	   System.out.println("file name="+file_name);
	        	   System.out.println("file path="+file_path);
	           }
	        }//for
	        
	    	if(stmt!=null)
	    		stmt.close();
	} catch (SQLException e) 
	{ 	e.printStackTrace(); }
	
}//insert

public String format(String current_table)
{
 
 String  new_table_name =  "";
 current_table = current_table.toLowerCase();
 //new_table_name  = current_table.replace("jboss_","");
 new_table_name  = current_table.replace("geronimo_","");
 
 return new_table_name;

}

public ArrayList<file_object> get_files(File branch_file)
{
    one_branch_info = null;
	one_branch_info   = new ArrayList<file_object>();
    
	parse_files(one_branch_info, branch_file);
    return one_branch_info;
 
}

//to get all this file_name  from the  required folder
// directory, .java file name, and file path.
//1. check if its directory, if yes call the recursive function
//2. if no, the if ir ends with .java extention then add it to the the one_branch_info() 
private void parse_files(ArrayList<file_object> one_branch_info, File branch_file) 
{
 
 File file_list [] =  branch_file.listFiles();
 for(int i =0; i<file_list.length;i++)
 {
      if(file_list[i].isDirectory())
      {
    	  parse_files(one_branch_info,file_list[i]);
      }//if
      
      else if(file_list[i].isFile())
      {
    	 if(file_list[i].toString().endsWith(".java"))
    	 {
    		 //table_name //Global variable
    		 String file_path=file_list[i].toString();
    		 
    		
    		 file_path =  file_path.replaceAll("/home/sangeeta/2Research/apache_data/","");
    		 int index = file_path.lastIndexOf("/");
    		 String file_name =file_path.substring(index+1).trim(); 
    		 
    		 int file_no=0;
    		 file_no = find_file_no(one_branch_info,file_name);
    		
    		 one_branch_info.add(new file_object(current_table,file_name,file_no,file_path));
    		 
    		/* System.out.println("Table ="+current_table);  
    		 System.out.println("ADD"+ file_name+ "  "+ file_path );*/
    		 
    	 }
    	 
      }//else if
 }

 
}//

//Return the correct file no, in case of duplicate entries
private int find_file_no(ArrayList<file_object> one_branch_info,String file_name) 
{
	
 int file_no = 0;
 int temp =-1;
 
 if(one_branch_info.size()!=0)
 {
    for(int i=0;i<one_branch_info.size();i++)
    {
	  String existing_file= one_branch_info.get(i).file_name;
	  if(existing_file.equalsIgnoreCase(file_name))
	  {
		  
		  int temp2 =  one_branch_info.get(i).file_no;
		  if(temp2>temp)
		  {
			  temp =temp2;
			  //System.out.println("i am in if");
		  }
		 /* if(file_name.equalsIgnoreCase("NotImplementedException.java"))
		  {
			  System.out.println("temp="+temp);
			  System.out.println("temp2"+temp2);
			  //intr();
		  }*/
	  }//if
	  
    }
    
 }
 
 file_no  = temp+1;

 return file_no;
	
}

/*
public void intr()
{
   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));	
   System.out.println("INTR");
   try {
	br.readLine();
} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
}*/


//main
public static void main(String[] args) 
{
		a_file_extraction afe =  new a_file_extraction();
		afe.conn=new db_conn().connect(afe.database, afe.user, afe.password);
		
		try
		{
			afe.table_names=afe.getTableNames(afe.rootDir);
			afe.parse(afe.table_names,afe.rootDir);
			
			if(afe.conn!=null)
				afe.conn.close();
		}
		catch (SQLException e) {
				e.printStackTrace();
		}
	}//main



}//class
